package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.exception.MerchantNotFoundException;
import com.sky.mapper.admin.AdminOrderMapper;
import com.sky.pojo.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    private AdminOrderMapper adminOrderMapper;


    /**
     * 订单搜索（分页查询）
     *
     * @param dto
     * @return
     */
    @Override
    public Result<PageResult> get(OrdersPageQueryDTO dto) {
        log.info("dto是{}", dto);
        //调用分页插件 执行动态 sql语句
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<OrderVO> pages = adminOrderMapper.get(dto);
        log.info("执行返回{}", pages);
        //封装对象
        PageResult pageResult = new PageResult();

        pageResult.setTotal(pages.getTotal());
        pageResult.setRecords(pages.getResult());

        return Result.success(pageResult);
    }


    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @Override
    public OrderStatisticsVO countStatus() {
        return adminOrderMapper.countStatus();
    }

    /**
     * 根据订单id查询订单详情
     * @param orderId
     * @return
     */
    @Override
    public OrderVO getOrder(Long orderId) {
        OrderVO vo = adminOrderMapper.getOrder(orderId);
        if (vo==null){
            throw new MerchantNotFoundException("----订单不存在------");
        }
        return vo;
    }

    /**
     * 取消订单
     *
     * @param dto
     */
    @Override
    public void quXiao(OrdersCancelDTO dto) {
        //获取订单信息 订单状态 1待付款 2 用户付款成功，等待商家接单  3.商家接单，等待骑手接单
        //   4 骑手接单成功-待取货  5待送达  6已完成  7已取消  8 退款中，取消中
        Orders orders = adminOrderMapper.getOrder(dto.getId());
        if (orders == null) {
            log.info("订单不存在");
            throw new MerchantNotFoundException("----订单不存在------");
        }
        //判断状态是
        Integer payStatus = orders.getPayStatus();
        Integer status = orders.getStatus();
        if (payStatus == 1 || status == 2 || status == 3 || status == 4 || status == 5 || status == 6) {
            log.info("执行退款");
            throw new MerchantNotFoundException("----订单状态不正确，无法取消--------");
        }
        orders.setCancelTime(LocalDateTime.now());
        orders.setPayStatus(Orders.CANCELLING);
        orders.setCancelReason(dto.getCancelReason());

        //数据表修改退款信息
        adminOrderMapper.quXiao(orders);
    }

    /**
     * 拒绝订单
     *
     * @param dto
     */
    @Override
    public void juJue(OrdersRejectionDTO dto) {
        //获取订单信息 订单状态 1待付款 2 用户付款成功，等待商家接单  3.商家接单，等待骑手接单
        //   4 骑手接单成功-待取货  5待送达  6已完成  7已取消  8 退款中，取消中
        Orders orders = adminOrderMapper.getOrder(dto.getId());
        if (orders == null) {
            log.info("订单不存在");
            throw new MerchantNotFoundException("----订单不存在------");
        }
        //判断状态是
        Integer payStatus = orders.getPayStatus();
        Integer status = orders.getStatus();
        if (payStatus == 1 || status == 2 || status == 3 || status == 4 || status == 5 || status == 6) {
            log.info("执行退款");
            throw new MerchantNotFoundException("----订单状态不正确，无法取消--------");
        }
        //设置订单 取消信息
        orders.setCancelTime(LocalDateTime.now());
        orders.setPayStatus(Orders.CANCELLING);
        orders.setRejectionReason(dto.getRejectionReason());
      //数据库修改
        adminOrderMapper.juJue(orders);
    }
}
